#include <bits/stdc++.h>
#include <cstdio>
#define MAXN 100001
using namespace std;
int n,m;
int ipt[MAXN];
int f[MAXN][20];
int lg2[MAXN];

void init(){
    for (int i=0;i<=n;i++){
        f[i][0]=ipt[i];
    }
    for (int i=2;i<=n;i++){
        lg2[i] = lg2[i >> 1]+1;
    }
    for (int j=1;j<=lg2[n];j++){
        for (int i=0;i+(1 << (j-1))<=n;i++){
            f[i][j]=max(f[i][j-1],f[i+(1 << (j-1))][j-1]);
        }
    }
}
int main(){
//    freopen("3865test.in","r",stdin);
    cin>>n>>m;
    ios::sync_with_stdio(false);
    for (int i=0;i<n;i++){
        cin>>ipt[i];
    }
    init();
    while (m--){
        int l,r;
        l--; r--;
        cin>>l>>r;
        int L = r-l+1;
        cout<<max(f[l][lg2[L]],f[r-(1 << lg2[L])][lg2[L]])<<'\n';
    }
    return 0;
}
